package com.hx78.mongo.uni;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
//import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.michaels.ifr.constants.ChannelType;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.io.IOException;
import java.time.LocalDateTime;

public class MongoQueryDeserialize {
    public static void main(String[] args) throws JsonProcessingException {
//        Query query1 = JSON.parseObject("{\"sorted\":false,\"sortObject\":{},\"meta\":{\"flags\":[]},\"queryObject\":{\"sku_number\":\"10702041\"},\"restrictedTypes\":[],\"limit\":100,\"skip\":0,\"fieldsObject\":{}}", Query.class);
//        Query query1 = JSON.parseObject("{\"restrictedTypes\":[],\"skip\":0,\"limit\":100,\"hint\":null,\"meta\":{\"flags\":[],\"cursorBatchSize\":null,\"allowDiskUse\":null,\"maxTimeMsec\":null,\"comment\":null},\"collation\":{\"empty\":true,\"present\":false},\"queryObject\":{\"sku_number\":\"10702041\"},\"fieldsObject\":{},\"sortObject\":{},\"sorted\":false}", Query.class);
//        MyQuery query2 = JSON.parseObject("{\"restrictedTypes\":[],\"skip\":0,\"limit\":100,\"hint\":null,\"meta\":{\"flags\":[],\"cursorBatchSize\":null,\"allowDiskUse\":null,\"maxTimeMsec\":null,\"comment\":null},\"collation\":{\"empty\":true,\"present\":false},\"queryObject\":{\"sku_number\":\"10702041\"},\"fieldsObject\":{},\"sortObject\":{},\"sorted\":false}", MyQuery.class);
//        MyQuery query3 = JSON.parseObject("{\"restrictedTypes\":[],\"skip\":0,\"limit\":100,\"hint\":null,\"meta\":{\"flags\":[],\"cursorBatchSize\":null,\"allowDiskUse\":null,\"maxTimeMsec\":null,\"comment\":null},\"collation\":{\"empty\":true,\"present\":false},\"queryObject\":{\"sku_number\":\"10702041\"},\"fieldsObject\":{},\"sortObject\":{},\"sorted\":false}", MyQuery.class);

//        Query query = new Query().addCriteria(Criteria.where("sku_number").is("10702041"));
//        Criteria criteria= new Criteria();
//        LocalDateTime date = LocalDateTime.now();
//        query.addCriteria(Criteria.where("status").in(SkuStatus.ACTIVE.getCode(), SkuStatus.SOLD_OUT.getCode()));
//        // com, b2b
//        Criteria criteria1 = Criteria.where("channel").in(ChannelType.COM.getCode(), ChannelType.B2B.getCode())
//                .orOperator(Criteria.where("online_only.available_to_date").lte(date), Criteria.where("online_only.available_from_date").gte(date));
//        // ea
//        Criteria criteria2 = Criteria.where("channel").is(ChannelType.ASSORTMENT.getCode()).and("online_only.available_to_date").lte(date);
//        query.addCriteria(criteria.orOperator(criteria1, criteria2));
//        query.limit(100);
//        query.skip(10);
//        query.fields().include("sku_number","item_name");
////        System.out.println(JSON.toJSON(query));
//
//        //创建一个jackson对象,用来解析数据
//        ObjectMapper mapper = new ObjectMapper();
//        mapper.registerModule(new Jdk8Module());
//        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
//        mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer());
//
//        //使用writeValueAsString方法将user转化为json对象
//        String jsonString = mapper.writeValueAsString(query);
//        System.out.println(jsonString);
//        Query newQuery = mapper.readValue(jsonString, Query.class);
//
//        MyQuery myQuery = (MyQuery) new MyQuery().addCriteria(Criteria.where("sku_number").is("10702041"));

        test();




    }

    public static void test() throws JsonProcessingException {
        Query query = new Query().addCriteria(Criteria.where("sku_number").is("10702041"));
//        Criteria criteria= new Criteria();
        LocalDateTime date = LocalDateTime.now();
//        query.addCriteria(Criteria.where("status").in(SkuStatus.ACTIVE.getCode(), SkuStatus.SOLD_OUT.getCode()));
//        // com, b2b
        Criteria criteria1 = Criteria.where("channel").in(ChannelType.COM.getCode(), ChannelType.B2B.getCode())
                .orOperator(Criteria.where("online_only.available_to_date").lte(date), Criteria.where("online_only.available_from_date").gte(date));
//        // ea
//        Criteria criteria2 = Criteria.where("channel").is(ChannelType.ASSORTMENT.getCode()).and("online_only.available_to_date").lte(date);
//        query.addCriteria(criteria.orOperator(criteria1, criteria2));
        query.addCriteria(criteria1);
        query.limit(100);
        query.skip(10);
        query.fields().include("sku_number","item_name");
        query.with(Sort.by(Sort.Direction.DESC, "_id"));


        ObjectMapper mapper = new ObjectMapper();
        mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer());
        mapper.registerModule(new JavaTimeModule());

        QueryForMongo queryForMongo = MongodbCommonHelper.query2QueryForMongo(query);
        String jsonString = mapper.writeValueAsString(queryForMongo);
        System.out.println(jsonString);
        QueryForMongo queryForMongo1 = mapper.readValue(jsonString, QueryForMongo.class);
        Query query1 = MongodbCommonHelper.queryForMongo2Query(queryForMongo1);

    }
}

class NullKeySerializer extends StdSerializer<Object> {
    public NullKeySerializer() {
        this(null);
    }

    public NullKeySerializer(Class<Object> t) {
        super(t);
    }

    @Override
    public void serialize(Object nullKey, JsonGenerator jsonGenerator, SerializerProvider unused)
            throws IOException, JsonProcessingException {
        jsonGenerator.writeFieldName("");
    }
}